package com.lq.educationsystem.student.service.impl;

import com.lq.educationsystem.common.util.*;
import com.lq.educationsystem.common.vo.StudentPageVo;
import com.lq.educationsystem.student.entiy.StudentTb;
import com.lq.educationsystem.student.mapper.StudentTbMapper;
import com.lq.educationsystem.student.service.StudentBaseInterface;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentBaseImpl implements StudentBaseInterface {
    //导入mapper
    @Autowired
    StudentTbMapper studentTbMapper;

    /**
     * 通过id查询
     * @param studentId
     * @return
     */
    @Override
    public Result queryById(Integer studentId) {
        StudentTb studentInfo = studentTbMapper.selectByPrimaryKey(Long.valueOf(studentId));
        return ResultBuild.buildOk(new Result<StudentTb>(),studentInfo);
    }

    /**
     * 新增用户
     * @param studentTb
     */
    @Override
    public void insertStudentInfo(StudentTb studentTb) {
        //新增学生
        studentTbMapper.insertSelective(studentTb);

    }

    /**
     * 更新学生信息
     * @param studentTb
     */
    @Override
    public void updateStudentInfo(StudentTb studentTb) {
        //更新学生信息
        int row = studentTbMapper.updateByPrimaryKeySelective(studentTb);
        System.out.println("跟新影响行数"+row);
        System.out.println(studentTb);
    }

    /**
     * 通过学生id删除用户信息
     * @param studentId
     */
    @Override
    public void deleteStudentInfo(Integer studentId) {
        studentTbMapper.deleteByPrimaryKey(Long.valueOf(studentId));
    }

    @Override
    public ResultPage<StudentTb> queryList(StudentPageVo studentPageVo) {
        //获取页面
        Integer pageSize = studentPageVo.getPageSize();
        Integer page = studentPageVo.getPage();
        int start = PageUtil.getStart(page, pageSize);

        //获取数据
        StudentTb studentTb = new StudentTb();

        BeanUtils.copyProperties(studentPageVo,studentTb);
        int counts=0;
        //TODO 这里可以编写  查询结果

        //调用mapper
        List<StudentTb>  studentTbList= studentTbMapper.queryList(start,pageSize,studentTb);
        //获取数据总数
        counts =studentTbMapper.queryCount(studentTb);
        //然后分页计算
        return ResultPageBuild.pageBuildOk(new ResultPage<StudentTb>(),page,pageSize,counts,studentTbList);


    }
}
